package com.tjetc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.entity.*;
import com.tjetc.mapper.ActivityFlowerMapper;
import com.tjetc.mapper.UserMapper;
import com.tjetc.service.IActivityFlowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjetc.service.IActivityService;
import com.tjetc.service.IUserMessageService;
import com.tjetc.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ActivityFlowerServiceImpl extends ServiceImpl<ActivityFlowerMapper, ActivityFlower> implements IActivityFlowerService {

    @Resource
    private  ActivityFlowerMapper activityFlowerMapper;

    @Resource
    private  IActivityFlowerService activityFlowerService;
    @Resource
    private IActivityService activityService;
    @Resource
    private IUserService userService;

    @Resource
    private UserMapper userMapper;
    @Resource
    private IUserMessageService userMessageService;
    @Override
    public PageInfo<ActivityFlower> lists(Integer curPage, Integer pageSize, Integer aid) {
        PageHelper.startPage(curPage,pageSize);
        List<ActivityFlower> list=activityFlowerMapper.list(aid);
        for (ActivityFlower activityFlower : list) {
            activityFlower.setActivity(activityService.getById(activityFlower.getActivityId()));
            activityFlower.setUser(userService.getById(activityFlower.getUserId()));
        }
        return new PageInfo<>(list);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(Integer id) {
        ActivityFlower activityFlower = activityFlowerService.getById(id);
        activityFlower.setState(1);
        activityFlowerMapper.updateById(activityFlower);
        //加分
        User user = userService.getById(activityFlower.getUserId());
        Activity activity=activityService.getById(activityFlower.getActivityId());
        user.setScore(user.getScore()+activity.getScore());
        userMapper.updateById(user);

        //加分消息
        UserMessage userMessage = new UserMessage();
        userMessage.setActivityId(activity.getId());
        userMessage.setUserId(user.getId());
        userMessage.setContent("你把报名的"+activity.getName()+"已发放"+activity.getScore()+"学分");
        userMessageService.save(userMessage);
        return true;
    }
}
